package gameEngine.com.state;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import gameEngine.com.*;
import gameEngine.com.Actor.Actor;
import gameEngine.com.Actor.Bullet;
import gameEngine.com.Actor.TankEnemy;
import gameEngine.com.Actor.TankMC;

public class StateGameplay extends MainActivity implements IConstant {

	public static Bitmap background = null;
	public static Sprite spriteDpad = null;
	public static Sprite spriteHud = null;
	// public static GameLayerBG gameLayerBG = new GameLayerBG();
	// public static GameLayerOB gameLayerOB = new GameLayerOB();
	public static TankMC tankMC;
	public static boolean showHint = true;
	// test
	// public static Enemy enemy;
	// public static Alien enemy2;
	public static ArrayList<Actor> listBullet = new ArrayList<Actor>();
	public static ArrayList<TankEnemy> listTankEmeny = new ArrayList<TankEnemy>();
	public static int countFrameToComplete = -1;
	public static int level = 3;

	// public static Bullet bullet;

	// endtest
	public static synchronized void SendMessageGameplay(int type) { 

		switch (type) {
		case MESSAGE_CTOR:
			
			TankEnemy.publicSprite = new Sprite("sprite/actor/mctank200_gray.sprt");
			listTankEmeny.add(new TankEnemy(600, 300, 12, 12, "Enemy", "tamk"));
			listTankEmeny.get(0).publicSprite.setAnim(listTankEmeny.get(0), 0, true, false);
			listTankEmeny.get(0).state = TankEnemy.STATE_AUTO_MOVE;
			spriteDpad = new Sprite("sprite/ui/dpad.sprt");
			if (tankMC == null)
				tankMC = new TankMC(350, 300, 12, 12, "MAINMC", "MAINMC");
			tankMC.state = TankMC.STATE_TRACK_MC_MOVE;
			countFrameToComplete = -1;
			tankMC.sprite = new Sprite("sprite/actor/mctank200_red.sprt");
			tankMC.sprite.setAnim(0, true, false);
			tankMC.UpdateCameraOffset();
			break;
		case MESSAGE_UPDATE:
			GameLayer.updatelayer();
			tankMC.update();
			
			if (listTankEmeny.size() == 0 && countFrameToComplete < 0 || tankMC.state == -1 && countFrameToComplete < 0 ) {
				countFrameToComplete = 20;
			}
			if (countFrameToComplete-- == 0) {
				changeState(STATE_COMPLETE_LEVEL);
			}
			for (int i = 0; i < listBullet.size(); i++) {
				if (listBullet.get(i).state == -1)
					listBullet.remove(i);
				else
					listBullet.get(i).update();
			}

			for (int i = 0; i < listTankEmeny.size(); i++) {
				if (listTankEmeny.get(i).state == -1)
					listTankEmeny.remove(i);
				else
					listTankEmeny.get(i).update();
			}

			if (isTouchReleaseInRect(SCREEN_WIDTH - 70, 10, 60, 60)) {
				changeState(STATE_IGM);
			}

			break;
		case MESSAGE_PAINT:

			GameLayer.drawLayerBackGround();
			GameLayer.drawLayerObject();
			// GameLayerOB.drawLayer(0, 0);
			tankMC.render();

			for (int i = 0; i < listTankEmeny.size(); i++) {
				listTankEmeny.get(i).render();
			}
			for (int i = 0; i < listBullet.size(); i++) {
				listBullet.get(i).render();
			}
			mainPaint.setColor(Color.RED);
			// bullet.sprite.drawAFrame(mainCanvas, 0, bullet.m_x, bullet.m_y);
			// mainCanvas.drawRect(bullet.m_x,bullet.m_y ,bullet.m_x + 1,
			// bullet.m_y+1,mainPaint);
			// bullet.sprite.drawAModule(mainCanvas,0,0,1,1,400,200);
			// bullet.sprite.drawAModule(mainCanvas,0,45,1,1,400,200);

			// bullet.sprite.drawAFrame(mainCanvas,0,0,1,1,200,200);
			// bullet.sprite.drawAFrame(mainCanvas,0,90,1,1,216,216);

			// bullet.sprite.drawAFrame(mainCanvas,0,180,1,1,200,200);

			drawHud();

			// end draw hud
			spriteDpad.drawAFrame(mainCanvas, 0, 100, 380);
			if (!(GameLib.touchState != MotionEvent.ACTION_MOVE && GameLib.touchState != MotionEvent.ACTION_DOWN) && !((Math.abs(GameLib.touchPosX - GameLib.s_dpadCenterX) > GameLib.DPAD_LENGTH) || (Math.abs(GameLib.touchPosY - GameLib.s_dpadCenterY) > GameLib.DPAD_LENGTH)))
				spriteDpad.drawAFrame(mainCanvas, 1, GameLib.touchPosX, GameLib.touchPosY);

			touch_drawAreaDpadDirection(GameLib.touchPosX, GameLib.touchPosY, GameLib.DPAD_LENGTH);
			// when touch in dpad

			// draw anim IGM icon
			if (isTouchDrapInRect(SCREEN_WIDTH - 70, 10, 60, 60))
				spriteIcon.drawAFrame(mainCanvas, 6, SCREEN_WIDTH - 70, 10);
			else
				spriteIcon.drawAFrame(mainCanvas, 5, SCREEN_WIDTH - 70, 10);
			break;
		case MESSAGE_DTOR:
			break;
		}
	}

	public static void drawHud() {
		// spriteBird.drawAModule(mainCanvas, 0, 102, 102);
		// draw hud
		mainCanvas.save(Canvas.CLIP_SAVE_FLAG);
		mainPaint.setStrokeWidth(0);
		// mainCanvas.drawRect(HUD_BLOOD_X, HUD_BLOOD_Y, HUD_BLOOD_X +
		// HUD_BLOOD_WIDTH, HUD_BLOOD_Y + HUD_BLOOD_HEIGHT, mainPaint);
		// mainCanvas.drawRect(HUD_MANA_X, HUD_MANA_Y, HUD_MANA_X +
		// HUD_MANA_WIDTH, HUD_MANA_Y + HUD_MANA_HEIGHT, mainPaint);
		mainCanvas.clipRect(HUD_BLOOD_X, HUD_BLOOD_Y, HUD_BLOOD_X + tankMC.m_blood, HUD_BLOOD_Y + HUD_BLOOD_HEIGHT);
		spriteHud.drawAFrame(mainCanvas, 1, 0, 0);
		mainCanvas.restore();
		mainCanvas.save(Canvas.CLIP_SAVE_FLAG);
		mainCanvas.clipRect(HUD_MANA_X, HUD_MANA_Y, HUD_MANA_X + tankMC.m_mana, HUD_MANA_Y + HUD_MANA_HEIGHT);
		// mainCanvas.clipRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
		spriteHud.drawAFrame(mainCanvas, 2, 0, 0);
		mainCanvas.clipRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
		mainCanvas.restore();
		spriteHud.drawAFrame(mainCanvas, 0, 0, 0);
	}

}
